package com.ruoyi.web.controller.counter;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.framework.web.domain.server.Sys;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.counter.domain.Vsrecord;
import com.ruoyi.counter.service.IVsrecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 版本更新记录Controller
 *
 * @author xll
 * @date 2021-08-25
 */
@RestController
@RequestMapping("/counter/vsrecord")
public class VsrecordController extends BaseController
{
    @Autowired
    private IVsrecordService vsrecordService;

    /**
     * 查询版本更新记录列表
     */
    @PreAuthorize("@ss.hasPermi('counter:vsrecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(Vsrecord vsrecord)
    {
        startPage();
        List<Vsrecord> list = vsrecordService.selectVsrecordList(vsrecord);
        return getDataTable(list);
    }

    /**
     * 导出版本更新记录列表
     */
    @PreAuthorize("@ss.hasPermi('counter:vsrecord:export')")
    @Log(title = "版本更新记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Vsrecord vsrecord)
    {
        List<Vsrecord> list = vsrecordService.selectVsrecordList(vsrecord);
        ExcelUtil<Vsrecord> util = new ExcelUtil<Vsrecord>(Vsrecord.class);
        return util.exportExcel(list, "版本更新记录数据");
    }

    /**
     * 获取版本更新记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('counter:vsrecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(vsrecordService.selectVsrecordById(id));
    }

    /**
     * 新增版本更新记录
     */
//    @PreAuthorize("@ss.hasPermi('counter:vsrecord:add')")
//    @Log(title = "版本更新记录", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody Vsrecord vsrecord)
//    {
//        return toAjax(vsrecordService.insertVsrecord(vsrecord));
//    }

    @PreAuthorize("@ss.hasPermi('counter:vsrecord:add')")
    @Log(title = "版本更新记录", businessType = BusinessType.INSERT)
 //   @ResponseBody
   // @RequestMapping(value = "/import",method = RequestMethod.POST)
    @PostMapping
     public AjaxResult importData(MultipartFile file, @RequestParam String  remark, @RequestParam String versionNo,
                                               @RequestParam String type, HttpServletRequest req) throws IOException {

    // public AjaxResult importData(MultipartFile file, @RequestBody Vsrecord vsrecord, HttpServletRequest req) throws IOException {
    //         public AjaxResult importData(MultipartFile file,  HttpServletRequest req) throws IOException {

      System.out.println(remark);
        System.out.println(versionNo);
        System.out.println(type);


        Vsrecord  vsrecord = new Vsrecord();
        System.out.println(vsrecord.toString());
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateNowStr = sdf.format(d);
        ///usr/local/apache-tomcat-8.5.45/webapps
        String realPath = "/usr/local/apache-tomcat-8.5.45/webapps/upLoadData/" + dateNowStr;
       // String realPath = "D:/upLoadData/" + dateNowStr;
        System.out.println(realPath);
        File folder = new File(realPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        String oldName = file.getOriginalFilename(); //app-relase.apk
        System.out.println(oldName);
        //截取.之前的字符串 app-relase
        String str1=oldName.substring(0, oldName.indexOf("."));
        //截取.之后的字符串  apk
        String str2=oldName.substring(str1.length()+1, oldName.length());
        //拼接文件名
        String  newName =  str1+vsrecord.getVersionno()+"-"+System.currentTimeMillis()+'.'+str2;
        System.out.println(newName);
        file.transferTo(new File(folder,newName));
        String hostNameAddress = InetAddress.getByName(req.getServerName()).getHostAddress();
        System.out.println(hostNameAddress);
        String urll = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() +"/upLoadData"+"/"+dateNowStr+"/"+ newName;
        System.out.println(urll);
        vsrecord.setUrl(urll);
        vsrecord.setRemark(remark);
        vsrecord.setVersionno(versionNo);
       // vsrecord.setName(name);
        Date date2 = new Date();
        System.out.println(date2);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateLocalNowStr = df.format(date2);

        vsrecord.setUploadtime(dateLocalNowStr);
        vsrecord.setType(Long.valueOf(type) );

        return toAjax(vsrecordService.insertVsrecord(vsrecord));


}


    /**
     * 修改版本更新记录
     */
    @PreAuthorize("@ss.hasPermi('counter:vsrecord:edit')")
    @Log(title = "版本更新记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Vsrecord vsrecord)
    {
        return toAjax(vsrecordService.updateVsrecord(vsrecord));
    }

    /**
     * 删除版本更新记录
     */
    @PreAuthorize("@ss.hasPermi('counter:vsrecord:remove')")
    @Log(title = "版本更新记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(vsrecordService.deleteVsrecordByIds(ids));
    }
}
